﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using MySql.Data;

namespace Financieras
{
    class clsMetodos
    {
        public static MySqlConnection conexion;

        public static MySqlConnection conectar()
        {
            conexion = new MySqlConnection(@"server=localhost;user id=root;password=1234;database=financieras");
            return conexion;
        }

        public string Operaciones(string operacion, Dictionary<int, MySqlParameter> diccionario)
        {
            try
            {
                MySqlCommand comando = new MySqlCommand(operacion, conectar());
                comando.CommandType = CommandType.StoredProcedure;

                foreach (KeyValuePair<int, MySqlParameter> parametro in diccionario)
                {
                    comando.Parameters.Add(parametro.Value);
                }
                comando.Connection.Open();
                int val = comando.ExecuteNonQuery();
                comando.Connection.Close();
                if (val > 0)
                {
                    return null;
                }
                else
                    return "No se ejecuto la Operacion. Verifique.";

            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

        public DataSet Consultas(string consulta)
        {
            DataSet ds = null;
            try
            {
                MySqlCommand comando = new MySqlCommand(consulta, conectar());
                comando.CommandType = CommandType.StoredProcedure;
                MySqlDataAdapter da = new MySqlDataAdapter(comando);
                ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {

                return null;
            }
        }

        public DataSet Consultas2(string consulta, Dictionary<int, MySqlParameter> diccionario)
        {
            DataSet ds = null;
            try
            {
                MySqlCommand comando = new MySqlCommand(consulta, conectar());
                comando.CommandType = CommandType.StoredProcedure;
                foreach (KeyValuePair<int, MySqlParameter> parametro in diccionario)
                {
                    comando.Parameters.Add(parametro.Value);
                }
                MySqlDataAdapter da = new MySqlDataAdapter(comando);
                ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {

                return null;
            }
        }


        //Try
        //    con.Open()
        //    cmd.CommandText = "select no_licitacion from licitacion where etapa<8 OR etapa='9B'"
        //    da.SelectCommand = cmd
        //    da.Fill(ds, "licitacion")
        //    ComboBoxLicita.DataSource = ds.Tables(0)
        //    ComboBoxLicita.DisplayMember = ds.Tables(0).Columns(0).Caption.ToString
        //    ds.Tables().Clear()
        //    con.Close()
        //Catch ex As Exception
        //    MsgBox("ERROR AL CARGAR LICITACIONES," + vbCrLf)
        //End Try

    }
}
